var ajaxResult = '';
var arrMonth = new Array();
arrMonth[0]="Jan";
arrMonth[1]="Feb";
arrMonth[2]="Mar";
arrMonth[3]="Apr";
arrMonth[4]="May";
arrMonth[5]="Jun";
arrMonth[6]="Jul";
arrMonth[7]="Aug";
arrMonth[8]="Sep";
arrMonth[9]="Oct";
arrMonth[10]="Nov";
arrMonth[11]="Dec";

var SITEURL = 'http://localhost:8080/';


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   sText = trim(sText);
	
   if (sText == '') {
      IsNumber = false;
   } else {
 
      for (i = 0; i < sText.length && IsNumber == true; i++)  { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) {
           IsNumber = false;
        }
      }
   }
   return IsNumber;
   
}

function doShowNewExpCatDialog() {
	document.getElementById("Overlay").style.visibility = 'visible'	
	var divLabel = document.getElementById("divNewExpCatLabel");
	var div = document.getElementById('NewExpCatDialog');
	divLabel.innerHTML = 'Please enter a new expense category:';
	var tbox = document.getElementById("txtNewExpCat");
	tbox.value = '';
	
	div.style.visibility = 'visible';

}

function doHideNewExpCatDialog() {
	document.getElementById("Overlay").style.visibility = 'hidden'	
	var div = document.getElementById('NewExpCatDialog');
	div.style.visibility = 'hidden';

	document.getElementById("ddlNewExpCategory").selectedIndex = 0;
	

}

function doSelectCategory(pValue) {
	if (pValue == 'new') {
		doShowNewExpCatDialog();
	}
}



function doAddNewExpCat()
{
	var tbox = document.getElementById("txtNewExpCat");
	var div = document.getElementById("divNewExpCatLabel");
	var catName = trim(tbox.value);
	var url = SITEURL + 'addcategory/';
	var param = '?catname=' + escape(catName);
	var rt = '';

	if (catName.length <= 0) {
		div.innerHTML = '<span class="RedLabel">No expense category specified. Please re-enter:</span>';
		tbox.focus();
		
	}
	else {
		doAjaxGet(url, param, 'addnewexpcat');
		

		
	}


}

function doAddNewExpCatCallBack(ajaxResult) {

	var tbox = document.getElementById("txtNewExpCat");
	var div = document.getElementById("divNewExpCatLabel");

	if (ajaxResult == 'duplicate') {
		div.innerHTML = '<span class="RedLabel">The new expense category you have entered already exists. Please re-enter:</span>'
		tbox.focus();
	}
	else if (ajaxResult == 'failure') {
		div.innerHTML = '<span class="RedLabel">An error occurred in saving the new expense category. Please try again later:</span>'
		tbox.focus();

	}
	else {
		var ddl = document.getElementById("ddlNewExpCategory");
		var newOpt = new Option(ajaxResult.toUpperCase(), ajaxResult.toUpperCase());
		ddl.options[ddl.length] = newOpt;
		
		doHideNewExpCatDialog();
		ddl.selectedIndex = ddl.length-1;
	}
}

function doAjaxGet(pUrl, pParam, pCallBackStr)
{ 
    	var xhr = null; 
    	var url = pUrl + pParam;
    	var result = '';

    	if (window.XMLHttpRequest)
	{
 		xhr = new XMLHttpRequest();

	} 
	else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
      }

    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) {
		     
		     ajaxResult = xhr.responseText;
			if (pCallBackStr == 'addnewexpcat') {
				doAddNewExpCatCallBack(ajaxResult);
			}
                
		  }
              else 
                 ajaxResult = 'failure';
         }
    }; 

   xhr.open('GET', url, true); 
   xhr.send(null); 
} 


function IsNewBudgetEmpty() {

	var amt = 0;
	var rt = true;

	for (var i=0;i<=11; i++) {
		
		val = document.getElementById('txt' + arrMonth[i] + 'NewB').value.replace('-', '');
		if (IsNumeric(val)) {
			amt = Math.abs(parseInt(val));
		} else {
			amt = 0;
		}
				
		if (amt > 0) {
			rt = false;			
		}
	}

	return rt;
}

function doPrepNewBudgetAmts() {

	var amt = 0;
	var val = '';
	var rt = true;

	for (var i=0;i<=11; i++) {

		val = document.getElementById('txt' + arrMonth[i] + 'NewB').value.replace('-', '');
		if (IsNumeric(val)) {
			amt = Math.abs(parseFloat(val));
		} else {
			amt = 0;
		}
		
		document.getElementById('txt' + arrMonth[i] + 'NewB').value = Math.round(amt) + '';
	}

	return rt;
}

function doSaveBudget() {

	var ddl = document.getElementById("ddlNewExpCategory");
	
	
	document.forms[0].txtBudgetAction.value = 'savebudget';

	if (!IsNewBudgetEmpty()) {
		if ((ddl.value == '') || (ddl.value == 'new')) {
			alert('Please select a expense category for the new budget allocation.');
			ddl.focus();
			return false;
		}
	}

	doPrepNewBudgetAmts();

	document.forms[0].submit();
	
	

}